<?php

namespace app\dao\office;

use app\dao\BaseDao;
use app\model\office\OfficeRules;

class OfficeRulesDao extends BaseDao
{

    public function index(array $where)
    {
        return $this->search()->with(['OfficeClass' => function ($query) {
            $query->withField(['id', 'title']);
        }])-> withSearch(['class_id'],['class_id' => $where['class_id']]) -> where('website_id', $where['website_id'])->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        if ($where['class_id']) {
            $data = [];
            foreach (explode(',', $where['class_id']) as $val) {
                $data = $this->getModel()->create(array_merge($where, ['class_id' => $val]));
            }
            return $data;
        }
    }

    public function updateDao(array $where)
    {
        return $this->search()->update($where);
    }

    public function deleteDao(array $where)
    {
        return $this->getModel()-> whereIn('id',$where['id']) -> update(['delete_time' => date('Y-m-d H:i:s', time())]);

    }

    protected function setModel(): string
    {
        return OfficeRules::class;
    }


    //------------------------------------------api---------------------------------------------------------
    /*
     * 获取课程规则
     */
    public function getRules(array $where){
        return $this->getModel()
            ->where('website_id',$where['website_id'])
            ->withoutField(['create_time','update_time','delete_time','website_id'])
            ->where('class_id',$where['class_id'])
            ->find();
    }
}